package com.familydoctor.listener;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.familydoctor.entity.*;
import com.familydoctor.util.StringCustomUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: tool
 * @description:
 * @author: tmz
 * @create: 2020-10-12 10:03
 */

public class ReadAllListener extends AnalysisEventListener<TypeSheetModel> {
    List<DetailModel> res = new ArrayList<>();
    Map<String, List<TypeSheetModel>> list = new TreeMap<>();
    TypeSheetModel preData = new TypeSheetModel();
    List<TypeSheetModel> temp = new ArrayList<>();

    public ReadAllListener() {

    }

    public ReadAllListener(List<DetailModel> res) {
        this.res = res;
    }

    @Override
    public void invoke(TypeSheetModel data, AnalysisContext context) {
        data.setType(context.readSheetHolder().getSheetName());
        if (StrUtil.isBlank(data.getQuestion())) {
            data.setName(StrUtil.isBlank(data.getName()) ? preData.getName() : data.getName());
            data.setQuestion(StrUtil.isBlank(data.getQuestion()) ? preData.getQuestion() : data.getQuestion());
//            data.setNumber(data.getNumber() != null && data.getNumber() > 0 ? data.getNumber() : preData.getNumber());
        } else {
            data.setName(StrUtil.isBlank(data.getName()) ? preData.getName() : data.getName());
            preData = data;
        }
        temp.add(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        List<DetailModel> collect = res.stream()
                .filter(detailModel -> StrUtil.equals(detailModel.getCategory()
                        , context.readSheetHolder().getSheetName()))
                .collect(Collectors.toList());
        List<TypeSheetModel> markings = temp.stream().filter(item -> StrUtil.isNotBlank(item.getScoreRanging()))
                .collect(Collectors.toList());
        collect.forEach(item -> {
            Map<String, ResultModel> res = new HashMap<>();
            List<ChoseModel> choseModels = new ArrayList<>();
            //答题结果 ，根据分数讲解
            markings.forEach(typeSheetModel -> {
                if (StrUtil.equals(item.getTitle(), typeSheetModel.getName())) {
                    ResultModel model = new ResultModel();
                    String srange = typeSheetModel.getScoreRanging();
                    if(srange.indexOf("以下")!=-1||srange.indexOf("少于")!=-1){
                        srange = StrUtil.format("-50,{}",StringCustomUtils.getNumByStr(srange));
                    }else if(srange.indexOf("以上")!=-1||srange.indexOf("分以上")!=-1){
                        srange = StrUtil.format("{},1000",StringCustomUtils.getNumByStr(srange));
                    }
                    srange = ReUtil.replaceAll(srange, "分", "");
                    srange = ReUtil.replaceAll(srange, "\\D+", ",");
                    model.setTl(typeSheetModel.getResult())
                            .setDetail(typeSheetModel.getConclusionOverview())
                            .setScore(srange);
                    res.put(srange, model);
                }
            });
            // 问题集合
            List<TypeSheetModel> collect2 = temp.stream()
                    .filter(typeSheetModel -> (StrUtil.equals(typeSheetModel.getName(),
                            item.getTitle()))&&(typeSheetModel.getNumber()!=null
                            &&typeSheetModel.getNumber()>0)).collect(Collectors.toList());
            choseModels = collect2.stream().map(typeSheetModel -> {
                ChoseModel choseModel = new ChoseModel();
                choseModel.setId(typeSheetModel.getNumber())
                        .setTitle(typeSheetModel.getQuestion());
                List<OptionModel> optionModels = new ArrayList<>();
                List<TypeSheetModel> collect1 = temp.stream().filter(typeSheetModeltwo ->
                        (StrUtil.equals(typeSheetModeltwo.getName(),
                                item.getTitle()))
                                &&StrUtil.equals(typeSheetModeltwo.getQuestion(),
                                typeSheetModel.getQuestion()))
                        .collect(Collectors.toList());
                collect1.forEach(typeSheetModelc1-> {
                    OptionModel optionModel = new OptionModel();
                    optionModel.setText(typeSheetModelc1.getOption())
                            .setScore(NumberUtil.parseInt(typeSheetModelc1.getScore()));
                    optionModels.add(optionModel);
                });
                choseModel.setOpts(optionModels);
                return choseModel;
            }).collect(Collectors.toList());
            item.setResult(res);
            item.setQueList(choseModels);
            item.setQuestions(choseModels.size());
        });
        Console.log("1111");
        //清空
        temp.clear();
    }
}
